package com.jxb.permissions;

import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class InvisibleFragment extends Fragment {
    PermissionCallback cb ;
    private final int REQUEST_CODE = 1;

    public void requestNow(String[] permissions,PermissionCallback callback){
        cb = callback;
        requestPermissions(permissions,REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode==REQUEST_CODE){
            List<String> deniedList = new ArrayList<>();
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i]!= PackageManager.PERMISSION_GRANTED){
                    deniedList.add(permissions[i]);
                }
            }
            boolean allGranted = deniedList.isEmpty();
            cb.requestSuccess(allGranted,deniedList);
        }
    }
}
